<template>
  <div class="thumbs-manage">
    <!-- 搜索框 -->
    <el-row :gutter="20">
      <el-col :span="6">
        <el-input
            v-model="searchQuery"
            placeholder="请输入用户ID或点赞ID"
            prefix-icon="el-icon-search"
            @keyup.enter.native="fetchData"
        ></el-input>
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="fetchData" style="margin-bottom: 20px;">搜索</el-button>
        <el-button @click="resetData" style="margin-bottom: 20px;">重置</el-button>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px;"
        :header-cell-style="{ background: '#ccc' }"
        border
    >
      <el-table-column
          prop="id"
          label="点赞ID"
          width="80"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="newsId"
          label="咨询ID"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="userId"
          label="用户ID"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="current"
          label="点赞时间"
          align="center"
      ></el-table-column>
      <el-table-column
          label="操作"
          width="160"
          align="center"
          fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="deleteThumbs(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import api from "@/api";
import {formatDate} from "@/utils/date";

export default {
  name: "ThumbsManage",
  data() {
    return {
      searchQuery: "", // 搜索关键词
      tableData: [], // 表格数据
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页显示条数
      total: 0, // 总条数
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    formatDate,
    resetData() {
      this.searchQuery = '';
      this.fetchData();
    },
    // 获取数据
    async fetchData() {
      // 模拟从后端获取数据
      const response = await api.thumbs.thumbsList();
      const mockData = response.data.data;
      // 根据搜索关键词过滤数据
      let filteredData = mockData.filter((item) =>
          item.userId.toString().includes(this.searchQuery) || item.id.toString().includes(this.searchQuery)
      );

      // 分页处理
      this.total = filteredData.length;
      this.tableData = filteredData.slice(
          (this.currentPage - 1) * this.pageSize,
          this.currentPage * this.pageSize
      );
    },
    // 每页显示条数改变时触发
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.fetchData();
    },
    // 当前页码改变时触发
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.fetchData();
    },
    // 删除点赞
    deleteThumbs(row) {
      this.$confirm("确定要删除该点赞吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除点赞逻辑
        api.thumbs.thumbsDelete(row.id).then(r => {
          if (r.data.code === 200) {
            this.$message.success('删除成功！');
            this.fetchData();
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    }
  }
};
</script>

<style scoped>
.thumbs-manage {
  padding: 20px;
}
</style>